package com.baseutils;

import android.util.Log;

import java.util.concurrent.LinkedBlockingQueue;

/**
 * 抽象队列处理工具类
 * 应用场景：适用于从一处读取数据，然后执行数据对应的任务。但是往往任务还没有执行完，又收到了数据，导致任务执行混乱。
 * 所以编写此类将读取的数据存入队列，然后依次从队列中取数据执行完任务再取下一个数据执行任务，依次类推。
 * T:可以自己封装数据
 *
 * @author XinYi
 */
public class AbsQueueTaskHelper<T> {
    protected final String TAG = this.getClass().getSimpleName();
    private boolean isTaskRunning = false;
    private OnTaskActivateListener onTaskActivateListener;
    private int alreadyFinishCount = 0;                                                    //记录已经执行的次数
    private long startTime;                                                                //起始时间，用于统计任务耗时时间。
    private boolean isFinish = true;                                                        //队列任务是否结束
    private LinkedBlockingQueue<T> queue = new LinkedBlockingQueue();
    private static AbsQueueTaskHelper instance = new AbsQueueTaskHelper();

    protected AbsQueueTaskHelper() {
        isTaskRunning = false;
        this.alreadyFinishCount = 0;
        this.isFinish = true;
    }

    public static AbsQueueTaskHelper getInstance() {
        return instance;
    }

    public void init() {
        isTaskRunning = false;
        this.isFinish = false;
        queue.clear();
    }

    /**
     * 将数据加入队列
     *
     * @param item
     */
    public void join(T item) {
//		Log.d(TAG,"加入队列--" + item);
        queue.offer(item);
        activateTask();
    }

    /**
     * 取队列数据执行任务
     */
    public void next() {
//		Log.d(TAG,"执行下一个任务");
        alreadyFinishCount++;
        isTaskRunning = false;
        activateTask();
    }


    /**
     * 激活队列任务
     */
    private void activateTask() {
        if (isTaskRunning == false) {
            if (queue.size() > 0) {
                isFinish = false;
                T item = queue.poll();
                if (onTaskActivateListener != null) {
                    isTaskRunning = true;
//					Log.d(TAG,"task running");
                    if (alreadyFinishCount == 0) {
                        startTime = System.currentTimeMillis();
                    }
                    onTaskActivateListener.onTaskActivate(item);

                }
            } else {
                isFinish = true;
            }
        } else {
            Log.d(TAG, "try to activateTask but there is a task running now");
        }
    }

    public void finish() {
        this.isFinish = true;
    }

    /**
     * 用于客户端调用自己的业务
     *
     * @param onTaskActivateListener
     */
    public void setOnTaskActivateListener(
            OnTaskActivateListener onTaskActivateListener) {
        this.onTaskActivateListener = onTaskActivateListener;
    }


    public interface OnTaskActivateListener<T> {
        void onTaskActivate(T item);
    }

    public int getAlreadyFinishCount() {
        return alreadyFinishCount;
    }

    public long getUsedTimeMilliSeconds() {
        return System.currentTimeMillis() - startTime;
    }

    public boolean isFinish() {
        return isFinish;
    }
}
